/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.acionista.view;

import br.jm.acionista.base.eao.EaoException;
import br.jm.acionista.entity.RelatorioCertificadoTemp;
import br.jm.acionista.entity.TipoTransferencia;
import br.jm.acionista.filter.Funcoes;
import br.jm.acionista.sistema.eao.RelatorioCertificadoEao;
import br.jm.acionista.sistema.eao.RelatorioCertificadoTempEao;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlSelectOneRadio;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author joao
 */
public class RelatorioLivroTransferenciaBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private FacesMessage msg = null;
    private List<RelatorioCertificadoTemp> certificadoList;
    private String cSql;
    private int impresso;
    private RelatorioCertificadoTempEao relatorioCertificadoTempJpa;
    private RelatorioCertificadoEao relatorioCertificadoJpa;
    private RelatorioCertificadoTemp[] selectedCertificadoCheck;
    private RelatorioCertificadoTemp selectedCertificadoTemp;
    private FacesContext ctx;
    public HttpSession session;
    private TipoTransferencia selectedTipoTransferencia;
    private String order = "R";
    private boolean imprimevia = true;
    private String tipovia = "1";
    private String numTransf1;
    private String numTransf2;
    private Date datrel = new Date();
    private Funcoes funcoes = new Funcoes();

    public RelatorioLivroTransferenciaBean() throws EaoException {
        relatorioCertificadoJpa = RelatorioCertificadoEao.getInstance();
        relatorioCertificadoTempJpa = RelatorioCertificadoTempEao.getInstance();

        numTransf1 = " ";
        numTransf2 = " ";

        impresso = 1;

        cSql = "select distinct b.id_cliente,a.nom_cliente "
                + "from cliente a, cautela b "
                + "where a.id_cliente = b.id_cliente "
                + "order by nom_cliente";

        certificadoList = relatorioCertificadoTempJpa.getNative("RelatorioCertificadoTemp", 0, relatorioCertificadoJpa.getCount("RelatorioCertificado"), null, true, null, cSql, null);
    }

    public RelatorioCertificadoTemp[] getSelectedCertificadoCheck() {
        return selectedCertificadoCheck;
    }

    public void setSelectedCertificadoCheck(RelatorioCertificadoTemp[] selectedCertificadoCheck) {
        this.selectedCertificadoCheck = selectedCertificadoCheck;
    }

    public List<RelatorioCertificadoTemp> getCertificadoList() {
        return certificadoList;
    }

    public void setCertificadoList(List<RelatorioCertificadoTemp> certificadoList) {
        this.certificadoList = certificadoList;
    }

    public RelatorioCertificadoTemp getSelectedCertificadoTemp() {
        return selectedCertificadoTemp;
    }

    public void setSelectedCertificadoTemp(RelatorioCertificadoTemp selectedCertificadoTemp) {
        this.selectedCertificadoTemp = selectedCertificadoTemp;
    }

    public void gerarTransferencia(ActionEvent actionEvent) {
        String cTransf = "";
        boolean lPassa = true;
        try {
            ctx = FacesContext.getCurrentInstance();
            session = (HttpSession) ctx.getExternalContext().getSession(false);

            if (tipovia.equals("2")) {
                if (!numTransf1.trim().isEmpty()) {
                    cTransf = "(" + numTransf1;
                } else {
                    lPassa = false;
                    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "ATENÇÃO ... Primeiro Numero de TransfeR6enia Obrigatório!", "");
                }

                if (lPassa) {
                    if (!numTransf2.trim().isEmpty()) {
                        cTransf = cTransf + "," + numTransf2;
                    }
                    cTransf = cTransf + ")";
/*
                    cSql = "select id_cliente,nom_cliente "
                            + "from relatorio_certificado_temp "
                            + "union "
                            + "( "
                            + "select b.id_cliente,a.nom_cliente "
                            + "from cliente a, cautela b "
                            + "where a.id_cliente = b.id_cliente and b.num_transferencia in ".concat(cTransf)
                            + " union "
                            + "select b.id_cliente,a.nom_cliente "
                            + "from cliente a, cautela_acionista b "
                            + "where a.id_cliente = b.id_cliente and b.num_transferencia in ".concat(cTransf)
                            + ") "
                            + "order by nom_cliente";

                    certificadoList = relatorioCertificadoTempJpa.getNative("RelatorioCertificadoTemp", 0, 2, null, true, null, cSql, null);

                    selectedCertificadoCheck = new RelatorioCertificadoTemp[2];
                    selectedCertificadoCheck[0] = certificadoList.get(0);
                    selectedCertificadoCheck[1] = certificadoList.get(1);
                    * 
                    */
                }
            }

            if (lPassa) {
                session.setAttribute("tabAcionistas", selectedCertificadoCheck);
                session.setAttribute("tipoTransf", this.selectedTipoTransferencia);
                session.setAttribute("datRel", funcoes.ConverteDateString(datrel));
                session.setAttribute("tipovia", this.tipovia);
                session.setAttribute("transf", cTransf);
                session.setAttribute("order", this.order);

                for (int i = 0; i < 600000000; i++) {
                    String cTeste = "";
                }
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Relatório Gerado com Sucesso!", "");
            }
        } catch (Exception ex) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro na Geração do Relatório!!", "");
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public TipoTransferencia getSelectedTipoTransferencia() {
        if (this.selectedTipoTransferencia == null) {
            this.selectedTipoTransferencia = new TipoTransferencia();
        }
        return selectedTipoTransferencia;
    }

    public void setSelectedTipoTransferencia(TipoTransferencia selectedTipoTransferencia) {
        this.selectedTipoTransferencia = selectedTipoTransferencia;
    }

    public void onRowSelectNavigateTipoTransf(SelectEvent event) {
        this.selectedTipoTransferencia = (TipoTransferencia) event.getObject();

    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public boolean isImprimevia() {
        return imprimevia;
    }

    public void setImprimevia(boolean imprimevia) {
        this.imprimevia = imprimevia;
    }

    public String getTipovia() {
        return tipovia;
    }

    public void setTipovia(String tipovia) {
        this.tipovia = tipovia;
    }

    public void atualizaForm(ValueChangeEvent event) {
        HtmlSelectOneRadio radio = (HtmlSelectOneRadio) event.getComponent();
        String valor = (String) radio.getValue();

        if (valor.equals("1")) {
            this.imprimevia = true;
        } else {
            this.imprimevia = false;
        }

    }

    public String getNumTransf1() {
        return numTransf1;
    }

    public void setNumTransf1(String numTransf1) {
        this.numTransf1 = numTransf1;
    }

    public String getNumTransf2() {
        return numTransf2;
    }

    public void setNumTransf2(String numTransf2) {
        this.numTransf2 = numTransf2;
    }

    public Date getDatrel() {
        return datrel;
    }

    public void setDatrel(Date datrel) {
        this.datrel = datrel;
    }
    
}
